<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="停车场id" prop="parkId">
      <el-input v-model="dataForm.parkId" placeholder="节点所属停车场id"></el-input>
    </el-form-item>
    <el-form-item label="硬件地址" prop="parkPointAddress">
      <el-input v-model="dataForm.parkPointAddress" placeholder="节点硬件地址"></el-input>
    </el-form-item>
    <el-form-item label="节点类型" prop="parkPointType">
      <el-input v-model="dataForm.parkPointType" placeholder="节点类型"></el-input>
    </el-form-item>
    <el-form-item label="节点状态" prop="parkPointStatus">
      <el-input v-model="dataForm.parkPointStatus" placeholder="节点状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          parkPointId: 0,
          parkId: '',
          parkPointAddress: '',
          parkPointType: '',
          parkPointStatus: '',
          addTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          parkId: [
            { required: true, message: '节点所属停车场id不能为空', trigger: 'blur' }
          ],
          parkPointAddress: [
            { required: true, message: '节点硬件地址不能为空', trigger: 'blur' }
          ],
          parkPointType: [
            { required: true, message: '节点类型不能为空', trigger: 'blur' }
          ],
          parkPointStatus: [
            { required: true, message: '节点状态不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '加入时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更改时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.parkPointId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.parkPointId) {
			this.$axios.get(`/parkpoint/info/${this.dataForm.parkPointId}`).then((data)=>{
				if (data && data.data.code === 0) {
					this.dataForm.parkId = data.data.parkPoint.parkId
					this.dataForm.parkPointAddress = data.data.parkPoint.parkPointAddress
					this.dataForm.parkPointType = data.data.parkPoint.parkPointType
					this.dataForm.parkPointStatus = data.data.parkPoint.parkPointStatus
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
			var params = {
				'parkPointId': this.dataForm.parkPointId || undefined,
				'parkId': this.dataForm.parkId,
				'parkPointAddress': this.dataForm.parkPointAddress,
				'parkPointType': this.dataForm.parkPointType,
				'parkPointStatus': this.dataForm.parkPointStatus
			}
			this.$axios.post(`/parkpoint/${!this.dataForm.parkPointId ? 'save' : 'update'}`, params).then((data)=>{
				if (data && data.data.code === 0) {
				  this.$message({
				    message: '操作成功',
				    type: 'success',
				    duration: 1500,
				    onClose: () => {
				      this.visible = false
				      this.$emit('refreshDataList')
				    }
				  })
				} else {
				  this.$message.error(data.data.msg)
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      }
    }
  }
</script>
